package br.ufc.customJDBC.Workaround;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

import br.ufc.customJDBC.routing.Router;

public class TempConnection implements Connection {

	private Connection connection;
	private Router router;
	Collection<Connection> connectionPool;

	public TempConnection(Connection connection, Collection<Connection> connectionPool, Router router){
		this.connection= connection;
		this.connectionPool = connectionPool;
		this.router = router;
	}

	@Override
	public PreparedStatement prepareStatement(String sql) {
		PreparedStatement ps = null;
		try {
			ps = new TempPreparedStatement(connection.prepareStatement(sql,
					ResultSet.TYPE_SCROLL_INSENSITIVE, 
					ResultSet.CONCUR_READ_ONLY), router);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return ps;
	}

	@Override
	public Statement createStatement() throws SQLException {
		return new TempStatement(connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
				,ResultSet.CONCUR_READ_ONLY), router);
	}	

	@Override
	public CallableStatement prepareCall(String sql) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public Statement createStatement(int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
					throws SQLException {		
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
					throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
					throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
			throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
			throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public PreparedStatement prepareStatement(String sql, String[] columnNames)
			throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}


	@Override
	public Statement createStatement(int resultSetType, int resultSetConcurrency)
			throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
		//		return connection.unwrap(iface);
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
		//		return connection.isWrapperFor(iface);
	}

	@Override
	public String nativeSQL(String sql) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
		//		return connection.nativeSQL(sql);
	}

	@Override
	public void setAutoCommit(boolean autoCommit) throws SQLException {
		for	(Connection c : connectionPool){	
			c.setAutoCommit(autoCommit);
		}		
	}

	@Override
	public boolean getAutoCommit() throws SQLException {
		return connection.getAutoCommit();
//		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void commit() throws SQLException {
		// TODO Auto-generated method stub
		connection.commit();
	}

	@Override
	public void rollback() throws SQLException {
		for	(Connection c : connectionPool){	
			c.rollback();
		}
	}

	@Override
	public void close() throws SQLException {
//		for	(Connection c : connectionPool){	
//			c.close();
//		}
	}

	@Override
	public boolean isClosed() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return connection.isClosed();
	}

	@Override
	public DatabaseMetaData getMetaData() throws SQLException {
//		throw new UnsupportedOperationException("Not supported yet.");
		return connection.getMetaData();
	}

	@Override
	public void setReadOnly(boolean readOnly) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		connection.setReadOnly(readOnly);
	}

	@Override
	public boolean isReadOnly() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return connection.isReadOnly();
	}

	@Override
	public void setCatalog(String catalog) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		connection.setCatalog(catalog);
	}

	@Override
	public String getCatalog() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return connection.getCatalog();
	}

	@Override
	public void setTransactionIsolation(int level) throws SQLException {
		for(Connection c : connectionPool){
			c.setTransactionIsolation(level);
		}
//		throw new UnsupportedOperationException("Not supported yet.");
//		connection.setTransactionIsolation(level);
	}

	@Override
	public int getTransactionIsolation() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return connection.getTransactionIsolation();
	}

	@Override
	public SQLWarning getWarnings() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");		
//		return connection.getWarnings();
	}

	@Override
	public void clearWarnings() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		connection.clearWarnings();
	}	

	@Override
	public Map<String, Class<?>> getTypeMap() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return connection.getTypeMap();
	}

	@Override
	public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		connection.setTypeMap(map);
	}

	@Override
	public void setHoldability(int holdability) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		connection.setHoldability(holdability);
	}

	@Override
	public int getHoldability() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return connection.getHoldability();
	}

	@Override
	public Savepoint setSavepoint() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return connection.setSavepoint();
	}

	@Override
	public Savepoint setSavepoint(String name) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return connection.setSavepoint(name);
	}

	@Override
	public void rollback(Savepoint savepoint) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		connection.rollback(savepoint);
	}

	@Override
	public void releaseSavepoint(Savepoint savepoint) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		connection.releaseSavepoint(savepoint);
	}	

	@Override
	public Clob createClob() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return connection.createClob();
	}

	@Override
	public Blob createBlob() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return createBlob();
	}

	@Override
	public NClob createNClob() throws SQLException {
		// TODO Auto-generated method stub
		return createNClob();
	}

	@Override
	public SQLXML createSQLXML() throws SQLException {
		// TODO Auto-generated method stub
		return connection.createSQLXML();
	}

	@Override
	public boolean isValid(int timeout) throws SQLException {
		// TODO Auto-generated method stub
		return connection.isValid(timeout);
	}

	@Override
	public void setClientInfo(String name, String value)
			throws SQLClientInfoException {
		// TODO Auto-generated method stub
		connection.setClientInfo(name, value);
	}

	@Override
	public void setClientInfo(Properties properties)
			throws SQLClientInfoException {
		// TODO Auto-generated method stub
		connection.setClientInfo(properties);
	}

	@Override
	public String getClientInfo(String name) throws SQLException {
		// TODO Auto-generated method stub
		return connection.getClientInfo(name);
	}

	@Override
	public Properties getClientInfo() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return getClientInfo();
	}

	@Override
	public Array createArrayOf(String typeName, Object[] elements)
			throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return createArrayOf(typeName, elements);
	}

	@Override
	public Struct createStruct(String typeName, Object[] attributes)
			throws SQLException {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not supported yet.");
//		return connection.createStruct(typeName, attributes);
	}

	@Override
	public void abort(Executor arg0) throws SQLException {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not supported yet.");		
	}

	@Override
	public int getNetworkTimeout() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
//		return 0;
	}

	@Override
	public String getSchema() throws SQLException {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void setNetworkTimeout(Executor arg0, int arg1) throws SQLException {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not supported yet.");

	}

	@Override
	public void setSchema(String arg0) throws SQLException {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not supported yet.");

	}
}
